package com.tuoyu.peopletest.view.rv;


import android.view.View;

import androidx.recyclerview.widget.LinearSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

public class MyLinearSnapHelper extends LinearSnapHelper {

    public View mTargetView;

    private RecyclerView mRv;

    public MyLinearSnapHelper(RecyclerView mRv) {
        this.mRv = mRv;
    }

    /*@Override
    public int[] calculateDistanceToFinalSnap(@NonNull RecyclerView.LayoutManager layoutManager, @NonNull View targetView) {

        if(mTargetView == null){
            mTargetView = targetView;
        }

        int[] ints = super.calculateDistanceToFinalSnap(layoutManager, mTargetView);
        mTargetView = null;
        return ints;
    }*/

    public void setTargerViewSmoothScroll(View targetView){
        mTargetView = targetView;
        if (mRv == null) {
            return;
        }
        RecyclerView.LayoutManager layoutManager = mRv.getLayoutManager();
        if (layoutManager == null) {
            return;
        }
        View snapView = findSnapView(layoutManager);
        if (snapView == null) {
            return;
        }
        int[] snapDistance = calculateDistanceToFinalSnap(layoutManager, mTargetView);
        if (snapDistance[0] != 0 || snapDistance[1] != 0) {
            mRv.smoothScrollBy(snapDistance[0], snapDistance[1]);
        }

    }
}
